﻿#-*- coding:utf-8 -*-
import tkinter.messagebox as box
import requests
import os



def cloud(path) : # 返回指定文件的云端目录
    path_up = os.getcwd()
    path_add = path_up + "\cot\path.cot"
    Cloud = open(path_add).read()
    str(path)
    if path[0] == "/" :
        return (Cloud + path[1:])
    else :
        return (Cloud + path)

def Networking_detection(): # 检查云端连接(数据库)
    try:
        cloud_path = cloud("Hi.cot")
        r = requests.get(cloud_path)
        with open(".\\tmp\\Hi.cot", "wb") as code:
            code.write(r.content)
    except :
        return (False)
    return (True)

def Get_archived(cloud_path,name) : # 下载云端数据
    if Networking_detection() == True :
        cloud_path = cloud(cloud_path)
        r = requests.get(cloud_path)
        if name[0] == "#" :
            name = ".\\tmp\\" + name[1:]
        try :
            with open(name, "wb") as code:
                code.write(r.content)
        except :
            box.showerror(title = "下载错误 Download error",
                          message = "下载 .cot 文件时出错\nError downloading .cot file")
            return (False)
        html = open(name).readline()
        if html == "<html>\n" :
            box.showerror(title="下载错误 Download error",
                          message="下载 .cot 文件时出错\nError downloading .cot file")
            return (False)
        return (True)
    else:
        box.showerror(title = "无法连接至云端 Networking Error",
                      message = "请检查网络连接状况\nPlease check the network connection")
        return (False)

def Del_file(path = r'.\\tmp') : # 删除临时文件
    ls = os.listdir(path)
    for i in ls:
        c_path = os.path.join(path, i)
        if os.path.isdir(c_path):
            del_file(c_path)
        else:
            os.remove(c_path)

def line_control(way) : # Iot连接时转换连接方式
    pass
    # 执行控制转换程序